package com.wl.DataBase;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.regex.Pattern;

/**
 * 注册信息校验工具类
 * @author TaiZai
 * @data 2024/10/9 19:31
 */
public class RegisterUtils {

    /**
     * 校验用户名是否符合规则
     * @param connection
     * @param username
     * @return
     */
    public static boolean checkUsername(Connection connection, String username) {
        Pattern pattern = Pattern.compile(UserRules.USERNAME_REGEX);
        if (!pattern.matcher(username).matches()) {
            System.out.println("用户名长度应该在2到8个字符！");
            return false;
        }

        String query = "select count(id) from users where username = ?";
        try (PreparedStatement preparedStatement  = connection.prepareStatement(query)) {
            preparedStatement.setString(1, username);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                int count = resultSet.getInt(1);
                if (count > 0) {
                    System.out.println("该用户名已被占用，请重新输入。");
                    return false;
                }
            }
        } catch (SQLException e) {
            System.out.println("查询用户时发生错误: " + e.getMessage());
        }
        return true;
    }

    /**
     * 密码校验是否符合规则
     * @param password
     * @return
     */
    public static boolean checkPassword(String password) {
        Pattern pattern = Pattern.compile(UserRules.PASSWORD_REGEX);
        if (!pattern.matcher(password).matches()) {
            System.out.println("密码长度应该在6到10个字符，且包含大小写字母和数字！");
            return false;
        }
        return true;
    }

    public static boolean checkPhone(String phone) {
        Pattern pattern = Pattern.compile(UserRules.PHONE_REGEX);
        if (!pattern.matcher(phone).matches()) {
            System.out.println("请检查您的手机号是否输入正确。");
            return false;
        }
        return true;
    }

    public static boolean checkEmail(String email) {
        Pattern pattern = Pattern.compile(UserRules.EMAIL_REGEX);
        if (!pattern.matcher(email).matches()) {
            System.out.println("只支持QQ邮箱，且首位为1，长度在11至19之间！");
            return false;
        }
        return true;
    }

    /**
     * 生成随机账号并校验
     * @param connection
     * @return
     */
    public static String generateAccount(Connection connection) {
        // 缓存数据库中所有的账号
        Set<String> accounts = new HashSet<>();
        String query = "select account from users";
        try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                accounts.add(resultSet.getString("account"));
            }
        } catch (SQLException e) {
            System.out.println("查询用户时发生错误: " + e.getMessage());
        }

        Random random = new Random();
        // 生成首位数字 [1,9]之间生成
        int firstDigit = random.nextInt(9) + 1;

        // 生成剩余9位数字
        StringBuilder account = new StringBuilder();
        account.append(firstDigit);
        for (int i = 0; i < 9; i++) {
            account.append(random.nextInt(10));
        }

        // 判断账号是否存在
        if (accounts.contains(account.toString())){
            return generateAccount(connection);
        }

        return account.toString();
    }
}
